package com.pdg.mcplugin.common.deprecated;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.logging.Logger;

import org.bukkit.Location;

public class BlockProtector {
	private final static String TABLE_BLOCKS = "%s_blocks";
	private final static String FIELD_BLOCK_WORLD = "block_world";
	private final static String FIELD_BLOCK_X = "block_x";
	private final static String FIELD_BLOCK_Y = "block_y";
	private final static String FIELD_BLOCK_Z = "block_z";
	private final static String FIELD_BLOCK_PROTECTION = "block_protection";

	private final static String DB_CHECK_FOR_BLOCKS_TABLE = "SELECT `name` FROM `sqlite_master` WHERE `type`='table' AND `name`='"+TABLE_BLOCKS+"';";
	private final static String DB_CREATE_BLOCKS_TABLE = "CREATE TABLE IF NOT EXISTS `"+TABLE_BLOCKS+"` (`"+FIELD_BLOCK_WORLD+"` TEXT,`"+FIELD_BLOCK_X+"` REAL,`"+FIELD_BLOCK_Y+"` REAL,`"+FIELD_BLOCK_Z+"` REAL,`"+FIELD_BLOCK_PROTECTION+"` INTEGER);";
	private final static String DB_CREATE_BLOCK = "INSERT INTO `"+TABLE_BLOCKS+"` (`"+FIELD_BLOCK_WORLD+"`,`"+FIELD_BLOCK_X+"`,`"+FIELD_BLOCK_Y+"`,`"+FIELD_BLOCK_Z+"`,`"+FIELD_BLOCK_PROTECTION+"`) VALUES ('%s',%f,%f,%f,%d)";
	private final static String DB_RETRIEVE_BLOCK = "SELECT `"+FIELD_BLOCK_PROTECTION+"` FROM `"+TABLE_BLOCKS+"` WHERE `"+FIELD_BLOCK_WORLD+"`='%s' AND `"+FIELD_BLOCK_X+"`=%f AND `"+FIELD_BLOCK_Y+"`=%f AND `"+FIELD_BLOCK_Z+"`=%f;";
	private final static String DB_UPDATE_BLOCK = "UPDATE `"+TABLE_BLOCKS+"` SET `"+FIELD_BLOCK_PROTECTION+"`=%d  WHERE `"+FIELD_BLOCK_WORLD+"`='%s' AND `"+FIELD_BLOCK_X+"`=%f AND `"+FIELD_BLOCK_Y+"`=%f AND `"+FIELD_BLOCK_Z+"`=%f;";
	private final static String DB_DELETE_BLOCK = "DELETE FROM `"+TABLE_BLOCKS+"` WHERE `"+FIELD_BLOCK_WORLD+"`='%s' AND `"+FIELD_BLOCK_X+"`=%f AND `"+FIELD_BLOCK_Y+"`=%f AND `"+FIELD_BLOCK_Z+"`=%f;";
	
	private final static String ERROR_COULD_NOT_CREATE_BLOCKS_TABLE = "[%s] Could not create blocks table.";
	
	private final static Integer PROTECTION_ZERO = 0;
	private final static Integer PROTECTION_MAXIMUM = 5;
	
	private Logger logger;
	private String pluginName;
	private String tablePrefix;
	public BlockProtector(Connection theConnection,Logger theLogger,String thePluginName,String theTablePrefix){
		setConnection(theConnection);
		setLogger(theLogger);
		setPluginName(thePluginName);
		setTablePrefix(theTablePrefix);
	}

	private void setTablePrefix(String theTablePrefix) {
		tablePrefix = theTablePrefix;
	}

	private void setPluginName(String thePluginName) {
		pluginName = thePluginName;
	}

	private void setLogger(Logger theLogger) {
		logger = theLogger;
	}

	private void setConnection(Connection theConnection) {
		connection = theConnection;
	}

	public void ensureBlocksTable() {
		try{
			Statement statement = getConnection().createStatement();
			ResultSet result = statement.executeQuery(String.format(DB_CHECK_FOR_BLOCKS_TABLE,getTablePrefix()));
			if(!result.next()){
				statement = getConnection().createStatement();
				statement.execute(String.format(DB_CREATE_BLOCKS_TABLE,getTablePrefix()));
			}
		}catch(Exception e){
			getLogger().info(String.format(ERROR_COULD_NOT_CREATE_BLOCKS_TABLE,getPluginName()));
			e.printStackTrace();
		}	}

	private String getTablePrefix() {
		return tablePrefix;
	}

	private Object getPluginName() {
		return pluginName;
	}

	private Logger getLogger() {
		return logger;
	}

	private Connection connection = null;
	private Connection getConnection() {
		return connection;
	}

	public boolean setBlockProtection(Location theLocation,
			Integer theProtectionLevel) {
		ensureBlocksTable();
		try{
			if(theProtectionLevel<=PROTECTION_ZERO){
				Statement statement = getConnection().createStatement();
				statement.execute(String.format(DB_DELETE_BLOCK,getTablePrefix(),theLocation.getWorld().getName(),theLocation.getX(),theLocation.getY(),theLocation.getZ()));
				return true;
			}else if(theProtectionLevel>PROTECTION_MAXIMUM){
				theProtectionLevel = PROTECTION_MAXIMUM;
			}
			Statement statement = getConnection().createStatement();
			statement.execute(String.format(DB_UPDATE_BLOCK,getTablePrefix(),theProtectionLevel,theLocation.getWorld().getName(),theLocation.getX(),theLocation.getY(),theLocation.getZ()));
			if(statement.getUpdateCount()==1) return true;
			statement = getConnection().createStatement();
			statement.execute(String.format(DB_CREATE_BLOCK,getTablePrefix(),theLocation.getWorld().getName(),theLocation.getX(),theLocation.getY(),theLocation.getZ(),theProtectionLevel));
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	public Integer getBlockProtection(Location theLocation) {
		ensureBlocksTable();
		try{
			Statement statement = getConnection().createStatement();
			ResultSet result = statement.executeQuery(String.format(DB_RETRIEVE_BLOCK,getTablePrefix(),theLocation.getWorld().getName(),theLocation.getX(),theLocation.getY(),theLocation.getZ()));
			if(result.next()){
				return result.getInt(FIELD_BLOCK_PROTECTION);
			}else{
				return PROTECTION_ZERO;
			}
		}catch(Exception e){
			e.printStackTrace();
			return PROTECTION_ZERO;
		}
	}

}
